<?php

namespace app\shop\controller;

use think\Controller;
// 引入邮箱发送类
use app\common\library\Email;

class Address extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->BusinessModel = model('Business.Business');
        $this->AddressModel = model('Business.Address');
        $this->RegionModel = model('Region');
        $this->business = [];
        // 查询用户是否存在
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $this->business=$this->BusinessModel->find($busid);
            if(!$this->business)
            {
                $this->error('用户不存在');
                exit;
            }
        }
    }
    
    public function index()
    {
        if($this->request->isPost())
        {
            $result=$this->AddressModel->where(['busid'=>$this->business->id])->select();
            if($result)
            {
                $this->success('收货地址',null,$result);
                exit;
            }else
            {
                $this->error('暂无收货地址',null,[]);
                exit;
            }
        }
    }
    public function toggle()
    {
        if($this->request->isPost())
        {
            $id=$this->request->param('id',0,'trim');

            // 先判断地址是否存在
            $address=$this->AddressModel->find($id);
            if(!$address)
            {
                $this->error('地址不存在');
                exit;
            }

            // 再判断地址是不是本人的
            if($address['busid'] != $this->business->id)
            {
                $this->error('不是你的地址');
                exit;
            }

            // 先更新所有地址为非默认地址
            $this->AddressModel->where(['busid'=>$this->business->id])->update(['status'=>'0']);
            
            // 再更新指定的默认地址
            $result=$this->AddressModel->where(['id'=>$id])->update(['status'=>'1']);
            if($result===false)
            {
                $this->error('更新默认地址失败');
                exit;
            }else
            {
                $this->success('更新默认地址成功');
                exit;
            }
        }
    }
    public function add()
    {
        if($this->request->isPost())
        {
            $params=$this->request->param();
            $code=$this->request->param('code',0,'trim');
            $status=$this->request->param('status',0,'trim');
            $params['status']=$status ? '1' : '0';

            // 判断是否为默认地址
            if($status)
            {
                // 将其所有的收货地址全部重置为非默认
                $this->AddressModel->where(['busid'=>$this->business->id])->update(['status'=>'0']);
            }

            // 先查询所选的地址
            if(!empty($code))
            {
                // 获取地区路径
                $parent=$this->RegionModel->where(['code'=>$code])->value('parentpath');

                // 转化为数组
                $list=explode(',',$parent);

                if(isset($list[0]))
                {
                    $params['province']=$list[0];
                }else
                {
                    $params['province']=null;
                }
                if(isset($list[1]))
                {
                    $params['city']=$list[1];
                }else
                {
                    $params['city']=null;
                }
                if(isset($list[2]))
                {
                    $params['district']=$list[2];
                }else
                {
                    $params['district']=null;
                }
            }

            // 插入数据
            $result=$this->AddressModel->validate("common/Business/Address")->save($params);

            if($result=== false)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }else
            {
                $this->success('添加收货地址成功');
                exit;
            }

        }
    }
    public function info()
    {
        if($this->request->isPost())
        {
            $id=$this->request->param('id',0,'trim');

            // 先判断地址是否存在
            $address=$this->AddressModel->find($id);
            if(!$address)
            {
                $this->error('地址不存在');
                exit;
            }

            // 再判断地址是不是本人的
            if($address['busid'] != $this->business->id)
            {
                $this->error('不是你的地址');
                exit;
            }
            $this->success('返回地址成功',null , $address);
            exit;
        }
    }
    public function edit()
    {
        if($this->request->isPost())
        {
            $params=$this->request->param();
            $code=$this->request->param('code',0,'trim');
            $status=$this->request->param('status',0,'trim');
            $params['status']=$status ? '1' : '0';
            $id=$this->request->param('id',0,'trim');

            // 先判断地址是否存在
            $address=$this->AddressModel->find($id);
            if(!$address)
            {
                $this->error('地址不存在');
                exit;
            }

            // 再判断地址是不是本人的
            if($address['busid'] != $this->business->id)
            {
                $this->error('不是你的地址');
                exit;
            }

            // 判断是否为默认地址
            if($status)
            {
                // 将其所有的收货地址全部重置为非默认
                $this->AddressModel->where(['busid'=>$this->business->id])->update(['status'=>'0']);
            }

            // 先查询所选的地址
            if(!empty($code))
            {
                // 获取地区路径
                $parent=$this->RegionModel->where(['code'=>$code])->value('parentpath');

                // 转化为数组
                $list=explode(',',$parent);

                if(isset($list[0]))
                {
                    $params['province']=$list[0];
                }else
                {
                    $params['province']=null;
                }
                if(isset($list[1]))
                {
                    $params['city']=$list[1];
                }else
                {
                    $params['city']=null;
                }
                if(isset($list[2]))
                {
                    $params['district']=$list[2];
                }else
                {
                    $params['district']=null;
                }
            }

            // 插入数据
            $result=$this->AddressModel->validate("common/Business/Address")->isUpdate(true)->save($params);

            if($result=== false)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }else
            {
                $this->success('编辑收货地址成功');
                exit;
            }

        }
    }

    public function del()
    {
        if($this->request->isPost())
        {
            $id=$this->request->param('id',0,'trim');

            // 先判断地址是否存在
            $address=$this->AddressModel->find($id);
            if(!$address)
            {
                $this->error('地址不存在');
                exit;
            }

            // 再判断地址是不是本人的
            if($address['busid'] != $this->business->id)
            {
                $this->error('不是你的地址');
                exit;
            }
            $result=$this->AddressModel->destroy($id);
            if($result === false)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }else
            {
                $this->success('删除收货地址成功');
                exit;
            }
        }
    }

    public function order()
    {
        // 切换选择地址
        $addrid=$this->request->param('addrid',0,'trim');
        
        $where=[
            'busid'=>$this->business->id,
            'id'=>$addrid
        ];
        $check=$this->AddressModel->where($where)->find();
        if($check)
        {
            $this->success('返回下单收货地址',null,$check);
            exit;
        }

        // 默认地址
        $where=[
            'busid'=>$this->business->id,
            'status'=>'1'
        ];
        $default=$this->AddressModel->where($where)->find();
        if($default)
        {
            $this->success('返回下单收货地址',null,$default);
            exit;
        }

        // 无默认地址，随机一个地址显示
        $where=[
            'busid'=>$this->business->id,
        ];
        $address=$this->AddressModel->where($where)->find();
        if($address)
        {
            $this->success('返回下单收货地址',null,$address);
            exit;
        }
    }
}